package com.redrose.demo.multiple;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.LayoutRes;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.redrose.demo.util.ContextUtil;

import java.util.List;

public abstract class BaseMultiLayoutAdapter<T> extends RecyclerView.Adapter<BaseMultipleViewHolder<T>> {
    protected List<T> mList;


    public BaseMultiLayoutAdapter(List<T> list) {
        this.mList = list;
    }

    @NonNull
    @Override
    public BaseMultipleViewHolder<T> onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return getViewHolder(parent, viewType);
    }

    protected abstract BaseMultipleViewHolder<T> getViewHolder(ViewGroup parent, int viewType);

    @Override
    public void onBindViewHolder(@NonNull BaseMultipleViewHolder<T> holder, int position) {
        T bean = mList.get(position);
        holder.bindView(bean);
    }

    @Override
    public int getItemCount() {
        return mList == null ? 0 : mList.size();
    }

    protected View getViewByLayoutRes(@NonNull ViewGroup parent, @LayoutRes int layoutRes) {
        Context context = ContextUtil.getInstance().getContext();
        LayoutInflater from = LayoutInflater.from(context);
        return from.inflate(layoutRes, parent, false);
    }
}
